package com.opc.work;

import cn.hutool.core.date.DateUtil;
import com.opc.entity.DeviceRecord;
import com.opc.entity.DeviceRecordLatest;
import com.opc.service.DeviceRecordLatestService;
import com.opc.service.DeviceRecordService;
import com.opc.service.DeviceService;
import com.opc.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author SEG
 */
@Slf4j
@Component
public class AbnormalAnalysisSchedule {
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    @Resource
    private DeviceRecordLatestService recordLatestService;
    @Resource
    private DeviceRecordService recordService;

    @Resource
    private DeviceService deviceService;

    @Resource
    RedisUtils redisUtils;

    @Scheduled(cron = "0 0 0/1 * * ? ")// 1小时统计一次
    public void opcReading() {
        System.out.println("现在时间：" + dateFormat.format(new Date()));
        List<String> dList = null;
        if (redisUtils.exists("code_key")) {
            dList = Arrays.asList(String.valueOf(redisUtils.get("code_key")).split(","));
        } else {
            dList = deviceService.selectAllDevices();
            String codesStr = String.join(",", dList);
            redisUtils.set("code_key", codesStr, 4 * 60 * 60L);
        }
        //2.从记录最新表中获取最新的设置值
        List<DeviceRecordLatest> latestListRecord = recordLatestService.selectAllLatestRecordByIds(dList);
        if (latestListRecord == null || latestListRecord.isEmpty()) {
            log.info("设备最新记录表为空");
            return;
        }
        Map<String, Integer> setMap = new HashMap<>();
        for (DeviceRecordLatest deviceRecordLatest : latestListRecord) {
            setMap.put(deviceRecordLatest.getDeviceCode(), deviceRecordLatest.getVal());
        }
        Object searchTimeObj = redisUtils.get("lastest_abnormal_time");
        Date searchTime = null;
        if (searchTimeObj != null) {
            searchTime = DateUtil.parse(searchTimeObj.toString(), "yyyy-MM-dd HH:mm:ss");
        }
        //1.获取记录表中的重置记录
        List<DeviceRecord> list = recordService.selectAllRecordListByIds(dList, searchTime);
        if (list == null || list.isEmpty()) {
            log.info("设备重置记录表为空");
            return;
        }
        List<Long> uIdList = new ArrayList<>();
        for (DeviceRecord record : list) {
            if (setMap.containsKey(record.getDeviceCode())) {
                //3.如果最新值和重置记录值中的值不一样，产生一条异常，即更新record_type 为1
                if (!Objects.equals(setMap.get(record.getDeviceCode()), record.getVal())) {
                    uIdList.add(record.getRecordId());
                }
            }
        }
        if (uIdList.isEmpty()) {
            log.info("没有需要更新的记录");
            return;
        }
        //更新记录的状态
        int uFlg = recordService.updateDeviceRecordByIds(uIdList);
        redisUtils.set("lastest_abnormal_time", DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        log.info("更新{}条记录", uFlg);
    }

}
